package com.unicloud.mail;

import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.MimeMessage;
import java.beans.SimpleBeanInfo;
import java.io.File;

public class SenderTest {
    public static void main(String[] args) throws Exception{
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost("smtp.163.com");
        mailSender.setProtocol("smtp");
        mailSender.setUsername("gaojiax2014@163.com");
        mailSender.setPassword("tomxin37");

        MimeMessage message = mailSender.createMimeMessage();

        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom("gaojiax2014@163.com");
            helper.setTo("1261368673@qq.com");
            helper.setSubject("from java client");
            helper.setText("how are u tom!!", true);

            FileSystemResource file = new FileSystemResource(new File("e:\\xx.md"));
            helper.addAttachment("喜喜.md", file);

            mailSender.send(message);
            System.out.println("带附件的邮件已经发送。");
        } catch (Exception e) {
            System.out.println("发送带附件的邮件时发生异常！");
            e.printStackTrace();
        }
    }
}
